package com.lqd.demo.springboot.orm.dataJPA.service;

import javax.annotation.Resource;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lqd.demo.springboot.orm.dataJPA.bean.UserLoginLog;
import com.lqd.demo.springboot.orm.dataJPA.repository.IUserLoginLogCrudRepository;
import com.lqd.demo.springboot.orm.dataJPA.repository.IUserLoginLogPageCrudRepository;
import com.lqd.demo.springboot.orm.dataJPA.repository.IUserLoginLogRepository;

@Service
public class UserLoginLogService {

	@Resource
	private IUserLoginLogRepository iUserLoginLogRepository ;
	
	@Resource
	private IUserLoginLogCrudRepository iUserLoginLogCrudRepository;
	
	@Resource
	private IUserLoginLogPageCrudRepository iUserLoginLogPageCrudRepository;
	
	public UserLoginLog findUserLoginLog(String userName)
	{
		return iUserLoginLogRepository.findByUsername(userName);
	}
	
	public UserLoginLog findMyUserLoginLog(String userName)
	{
		return iUserLoginLogRepository.findByMyUsername(userName);
	}
	
	@Transactional
	public void save(UserLoginLog userLoginLog) {
		
		iUserLoginLogCrudRepository.save(userLoginLog);
	}
	
	@Transactional
	public void delete(int id){
		iUserLoginLogCrudRepository.delete(id);
	}
	
	public Iterable<UserLoginLog> getAll(int pageNo ,int pageSize){
		
		return iUserLoginLogPageCrudRepository.findAll(new PageRequest(pageNo, pageSize));
	}
	
	public Iterable<UserLoginLog> getAll2(int pageNo ,int pageSize,String orderby){
		
		Sort sort = new Sort(Sort.DEFAULT_DIRECTION.DESC,orderby);
		return iUserLoginLogPageCrudRepository.findAll(sort);
	}
}
